/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sisfo.astacala.controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import sisfo.astacala.model.ModelUser;
import sisfo.astacala.object.ObjectUser;
import sisfo.astacala.util.UtilGeneral;
import sisfo.astacala.util.UtilPasswordEncryptor;

/**
 *
 * @author arif
 */
public class GeneralController extends ActionSupport implements ServletRequestAware {

    // <editor-fold desc="atribute">
    private HttpServletRequest servletRequest;
    private String message;
    private InputStream inputStream;
    private static final Logger logger = Logger.getLogger(GeneralController.class.getName());
    // </editor-fold>

    // <editor-fold desc="index controller">
    public String indexController() {
        Map session = ActionContext.getContext().getSession();
        if (session.get("user") != null) {
            session.remove("user");
            session.remove("menu");
        }
        return SUCCESS;
    }
    // </editor-fold>

    // <editor-fold desc="login controller">
    public String loginController() {
        try {
            String password = UtilPasswordEncryptor.getInstance().encryptPassword(servletRequest.getParameter("password"));
            String query = String.format("from %s where username='%s' and password='%s'",
                    "ObjectUser",
                    servletRequest.getParameter("username"),
                    password);
            List userList = (List) ModelUser.getInstance().searchModel(query);

            if (userList.size() > 0) {
                message = SUCCESS;
                Map session = ActionContext.getContext().getSession();

                servletRequest.getSession().setAttribute("user", servletRequest.getParameter("username"));
                servletRequest.getSession().setAttribute("parent", ((ObjectUser) userList.get(0)).getDivision().toString());
            } else {
                message = ERROR;
            }
        } catch (Exception ex) {
            message = ERROR;
            ex.printStackTrace();
        }
        return message;
    }
    // </editor-fold>
    
    // <editor-fold desc="logout controller">
    public String logoutController() {
        Map session = ActionContext.getContext().getSession();
        if (session.get("user") != null) {
            session.remove("user");
            session.remove("menu");
        }
        return SUCCESS;
    }
    // </editor-fold>

    // <editor-fold desc="generate menu controller">
    public String generateMenu() {
        Map session = ActionContext.getContext().getSession();
        inputStream = IOUtils.toInputStream(UtilGeneral.getInstance().generateMenu(session.get("parent").toString()));
        return SUCCESS;
    }
    // </editor-fold>

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        servletRequest = hsr;
    }

    public InputStream getInputStream() {
        return inputStream;
    }
}
